package com.itheima.manage.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.commons.pojo.entity.CommentUser;
import com.itheima.commons.pojo.mongo.Comment;
import com.itheima.commons.vo.backstage.CommentBlackVo;
import com.itheima.commons.vo.backstage.KeepVo;
import com.itheima.commons.vo.reception.PageResult;
import com.itheima.dubbo.interfaces.CommentApi;
import com.itheima.dubbo.interfaces.KeepApi;
import com.itheima.dubbo.interfaces.UserInfoApi;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
public class ManageService {
    @Reference
    private CommentApi commentApi;

    @Reference
    private UserInfoApi userInfoApi;

    @Reference
    private KeepApi keepApi;


    //获取评论列表
    public CommentBlackVo getCommectList(Map<String, Object> map, String pagesize, String page, String sortProp, String sortOrder, String messageID) {
        Object userID = map.get("userID");
        Object reasonsForThawing = map.get("reasonsForThawing");
        //根据传过来的类型进行排序分类
        PageResult pageResult = null;

        if (sortOrder.equals("ascending")) {
            pageResult = commentApi.getAscCommenctList(userID, pagesize, page, sortProp, sortOrder, messageID);
        } else {
            pageResult = commentApi.getCommenctList(userID, pagesize, page, sortProp, sortOrder, messageID);
        }

        //拼凑vo对象
        CommentBlackVo commentBlackVo = new CommentBlackVo();

        commentBlackVo.setCounts(Convert.toInt(pageResult.getCounts()));
        commentBlackVo.setPagesize(pageResult.getPagesize());
        commentBlackVo.setPages(Convert.toInt(pageResult.getPages()));
        commentBlackVo.setPage(Convert.toInt(pageResult.getPage()));

        List<?> items = pageResult.getItems();
        List<Comment> comments = (List<Comment>) items;
        List<CommentUser> commentUsers = new ArrayList<>();
        for (Comment comment : comments) {

            CommentUser commentUser = new CommentUser();

            commentUser.setId(Convert.toStr(comment.getId()));
            commentUser.setUserId(Convert.toInt(comment.getUserId()));
            commentUser.setContent(comment.getContent());
            commentUser.setCreateDate(Convert.toInt(comment.getCreated()));
            //根据评论人id查询评论人昵称
            Long userId = comment.getUserId();
            String nickName = userInfoApi.findNickNameByUserId(userId);
            commentUser.setNickname(nickName);
            commentUsers.add(commentUser);
        }

        //将集合添加到vo中
        commentBlackVo.setItems(commentUsers);

        return commentBlackVo;

    }

    //数据统计之注册存留
    public List<KeepVo> getkeeps(Date before, Date after, String sex) {

        // 不传默认为当前日 当前月
        if (ObjectUtil.isNull(before) || ObjectUtil.isNull(after)) {
            after = DateUtil.beginOfDay(new Date());
            before = DateUtil.beginOfMonth(new Date());
        }

        long timeInterval = after.getTime() - before.getTime();

        List<KeepVo> keepVos = new ArrayList<>();
        //KeepVo keepVo = new KeepVo();
        //写一个循环查询指定日期的数据
        //计算查询几天
        int days = Convert.toInt(timeInterval / 1000 / 60 / 60 / 24);
        //差几天循环几次
        for (int i = 0; i < days; i++) {
            KeepVo keepVo = new KeepVo();
            //获取时间段，天为单位
            Date date1 = addOneDay(before, i);
            Date date2 = addOneDay(before, i + 1);
//            System.out.println(date1);
//            System.out.println(date2);
            //查询当天的注册人数
            int keep = keepApi.getNumBydate(date1, date2, sex);
//            System.out.println("当天注册人数"+keep);
            //查询当天注册人的id
            List<Long> keepIds = keepApi.getIdbycreattime(date1, date2, sex);
            //新建一个集合用来存第n天的登录人数
            List<Integer> logs = new ArrayList<>();
            //循环6次，
            for (int k = 1; k <= 7; k++) {
                //计算每一次的时间
                Date date3 = addOneDay(date1, k);
                Date date4 = addOneDay(date2, k);
//                System.out.println(date3);
//                System.out.println(date4);
                //通过时间获取id的集合
                List<Long> logids = keepApi.getLogNum(date3, date4, sex);
//                System.out.println("第"+date3+"id的集合"+logids);
                //通过求交集来判断数量
                Set<Long> longs = CollUtil.intersectionDistinct(keepIds, logids);
                int logNum = longs.size();
//                System.out.println("第"+k+"天的登录人数"+logNum);
                //将登录人的数量添加到集合中
                logs.add(logNum);
            }
            //计算第30天的
            Date date5 = addOneDay(date1, 30);
            Date date6 = addOneDay(date2, 30);
            List<Long> logids = keepApi.getLogNum(date5, date6, sex);
            //通过求交集来判断数量
            Set<Long> longs = CollUtil.intersectionDistinct(keepIds, logids);
            int logNum = longs.size();
            logs.add(logNum);

            //数据添加到vo中
            keepVo.setTime(date1);
            keepVo.setKeeps(keep);

            if (logs.get(0) == 0) {
                keepVo.setDay1(0.00);
            } else {
                keepVo.setDay1(NumberUtil.round(Convert.toDouble(logs.get(0)) / Convert.toDouble(keep), 2).doubleValue());
//                System.out.println(Convert.toDouble(logs.get(0)));
            }
            if (logs.get(1) == 0) {
                keepVo.setDay2(0.00);
            } else {
                keepVo.setDay2(NumberUtil.round(Convert.toDouble(logs.get(1)) / Convert.toDouble(keep), 2).doubleValue());
            }
            if (logs.get(2) == 0) {
                keepVo.setDay3(0.00);
            } else {
                keepVo.setDay3(NumberUtil.round(Convert.toDouble(logs.get(2)) / Convert.toDouble(keep), 2).doubleValue());
            }
            if (logs.get(3) == 0) {
                keepVo.setDay4(0.00);
            } else {
                keepVo.setDay4(NumberUtil.round(Convert.toDouble(logs.get(3)) / Convert.toDouble(keep), 2).doubleValue());
            }
            if (logs.get(4) == 0) {
                keepVo.setDay5(0.00);
            } else {
                keepVo.setDay5(NumberUtil.round(Convert.toDouble(logs.get(4)) / Convert.toDouble(keep), 2).doubleValue());
            }
            if (logs.get(5) == 0) {
                keepVo.setDay6(0.00);
            } else {
                keepVo.setDay6(NumberUtil.round(Convert.toDouble(logs.get(5)) / Convert.toDouble(keep), 2).doubleValue());
            }
            if (logs.get(6) == 0) {
                keepVo.setDay7(0.00);
            } else {
                keepVo.setDay7(NumberUtil.round(Convert.toDouble(logs.get(6)) / Convert.toDouble(keep), 2).doubleValue());
            }
            if (logs.get(7) == 0) {
                keepVo.setDay30(0.00);
            } else {
                keepVo.setDay30(NumberUtil.round(Convert.toDouble(logs.get(7)) / Convert.toDouble(keep), 2).doubleValue());
            }
            keepVos.add(keepVo);
        }
        return keepVos;
    }

    //获得下一天的时间
    public Date addOneDay(Date time, int i) {
        long time1 = time.getTime();
        long l = time1 + (24 * 60 * 60 * 1000L * i);
        Date date = new Date(l);
        return date;
    }
}